// 统计分析相关的actions
export default {
  // 获取下载统计数据
  async fetchDownloadStats({ commit, state, dispatch }) {
    try {
      commit('SET_LOADING', true)
      dispatch('setLoading', true)
      
      // 调用API获取下载统计数据
      const response = await this.app.$api.stats.getDownloadStats(state.filters)
      
      // 设置下载统计数据
      commit('SET_DOWNLOAD_STATS', response.data || [])
    } catch (error) {
      console.error('Failed to fetch download stats:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('获取下载统计数据失败')
    } finally {
      commit('SET_LOADING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 获取热门包数据
  async fetchPopularPackages({ commit, state, dispatch }) {
    try {
      commit('SET_LOADING', true)
      dispatch('setLoading', true)
      
      // 调用API获取热门包数据
      const response = await this.app.$api.stats.getPopularPackages(state.filters)
      
      // 设置热门包数据
      commit('SET_POPULAR_PACKAGES', response.data || [])
    } catch (error) {
      console.error('Failed to fetch popular packages:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('获取热门包数据失败')
    } finally {
      commit('SET_LOADING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 获取趋势数据
  async fetchTrendData({ commit, state, dispatch }) {
    try {
      commit('SET_LOADING', true)
      dispatch('setLoading', true)
      
      // 调用API获取趋势数据
      const response = await this.app.$api.stats.getTrendData(state.filters)
      
      // 设置趋势数据
      commit('SET_TREND_DATA', response.data || [])
    } catch (error) {
      console.error('Failed to fetch trend data:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('获取趋势数据失败')
    } finally {
      commit('SET_LOADING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 更新过滤器并重新获取数据
  async updateFilters({ commit, dispatch }, filters) {
    commit('SET_FILTERS', filters)
    await Promise.all([
      dispatch('fetchDownloadStats'),
      dispatch('fetchPopularPackages'),
      dispatch('fetchTrendData')
    ])
  },
  
  // 清空统计数据
  clearStats({ commit }) {
    commit('CLEAR_STATS')
  },
  
  // 同时获取所有统计数据
  async fetchAllStats({ dispatch }) {
    await Promise.all([
      dispatch('fetchDownloadStats'),
      dispatch('fetchPopularPackages'),
      dispatch('fetchTrendData')
    ])
  }
}